#!/usr/bin/perl -w

use strict;

die "mark_sam_qual0_start_end.pl <input.sam> <out.sam>\n" unless $ARGV[1];

open SAM0, '<', "$ARGV[0]";
open SAM, '>', "$ARGV[1]";

my ($qual0, $start_len, $end_len, $str_len_min) = ("!", 21, 25, 66);
my $qual0_start, my $qual0_end;

foreach (0..$start_len-1) {
	$qual0_start .= $qual0; 
}
foreach (0..$end_len-1) {
	$qual0_end .= $qual0; 
}
print $qual0_start . "\n";
print $qual0_end . "\n";

my @sam_line;
while (<SAM0>) {
	if (/^\@/) {
		print SAM;
	}else {
		# chomp;
		@sam_line = split /\t/;
		my $str_len = length $sam_line[10];
		if ($str_len >= $str_len_min) {
			my $qual = substr $sam_line[10], 0, $str_len - $end_len;
			$qual = substr $qual, $start_len;
			$sam_line[10] = $qual0_start . $qual . $qual0_end;
			print SAM join "\t", @sam_line;
		}
		
	
	}

} 

close SAM0;
close SAM;
